package com.sdkx.app.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.sdkx.app.R;
import com.sdkx.app.model.AirWay;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;


public class RoutesListAdapter extends BaseAdapter {
    private Context context;
    private List<AirWay> airWays = new ArrayList<AirWay>();
    public RoutesListAdapter(Context context) {
        this.context = context;
    }

    @Override
    public int getCount() {
        return airWays.size();
    }

    @Override
    public Object getItem(int i) {
        return airWays.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup viewGroup) {
        ViewHolder holder;
        if (convertView == null) {
            convertView =  View.inflate(context, R.layout.routelist_item, null);
            holder = new ViewHolder();
            holder.tv_three_menu_name = (TextView) convertView.findViewById(R.id.tv_wpname);
            holder.iv_three_menu_icon = (ImageView) convertView.findViewById(R.id.iv_three_menu_icon);
            convertView.setTag(holder);
            convertView.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,80));
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        AirWay temp_route = airWays.get(position);
        holder.tv_three_menu_name.setText(temp_route.getName());
        if(temp_route.getIsEdit()){
            holder.iv_three_menu_icon.setImageResource(R.drawable.unlock);
        }else{
            holder.iv_three_menu_icon.setImageResource(R.drawable.lock);
        }
        return convertView;
    }

    public Context getContext() {
        return context;
    }

    public void setContext(Context context) {
        this.context = context;
    }

    public List<AirWay> getList() {
        return airWays;
    }

    public void setList(List<AirWay> list) {
        airWays = list;
        Collections.sort(airWays, new Comparator<AirWay>() {
            @Override
            public int compare(AirWay o1, AirWay o2) {
                return o1.getName().compareTo(o2.getName());
            }
        });
        this.notifyDataSetChanged();
    }


    public static class ViewHolder {
        public TextView tv_three_menu_name;
        public ImageView iv_three_menu_icon;
    }

}
